package com.mi.proxy4;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test3 {

	public static void main(String[] args) throws Exception {
		Jay jay = new Jay();
		Class<?>[] interfaces = jay.getClass().getInterfaces();
		Object obj = Proxy.newProxyInstance(jay.getClass().getClassLoader(), interfaces, new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				if(method.getName().equals("sign")){
					System.out.println("方文山代理周杰伦签约华裔兄弟");
					return null;
				}else if(method.getName().equals("sing")){
					System.out.println("大家好，周杰伦，请大家多多支持！");
					Object value = method.invoke(jay, args);
					System.out.println("谢谢大家！");
					return value;
				}else if(method.getName().equals("lives")){
					try {
						return method.invoke(jay, args);
					} catch (Exception e) {
						System.out.println("好好生活嘛，父亲之间不要有隔夜仇！");
						throw e;
					}
				}else {
					return method.invoke(jay, args);
				}
				
			}
		});
		
		System.out.println(obj.getClass());
		System.out.println(obj instanceof Singer);
		System.out.println(obj instanceof Person);
		
		Singer singer = (Singer)obj;
		singer.sing("七里香");
		singer.sign();
		singer.work();
		
		Person person = (Person)obj;
		int index = person.lives();
		System.out.println(index);
	}

}
